package interfaces.admin;


import java.util.ArrayList;
import java.util.ResourceBundle;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.ControlListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
import aplicacion.Vista;

public class TabDepartamentosAdmin extends Thread{
	private Table tablaInfo;
	private boolean datosInterfazCargados;
	final Vista vista;
	final ResourceBundle bundle;
	final TabFolder tabFolder;
	private Combo cmbDepartamentos;
	final Shell padre;

	
	public TabDepartamentosAdmin(TabFolder tabFolder, Vista vista, ResourceBundle bundle,Shell padre) {
		this.vista = vista;
		this.bundle = bundle;
		this.tabFolder = tabFolder;
		this.padre=padre;
		crearTabAdminDepartamentos();
	}
	
	
	
	
	
	/**
	 * Implementa un hilo que coge los empleados del departamento del servidor.
	 */
	public void run() {
		try {
			while (!vista.isCacheCargada()) {
				sleep(5000);
			}
		} catch (Exception e) {
		}
		datosInterfazCargados = true;
		// while (run) {

		if (tabFolder.isDisposed()) {
		} // run = false;
		else {
			if (!tabFolder.isDisposed()) {
				// Actualizar tabla
				if (!tabFolder.isDisposed()) {
					tabFolder.getDisplay().asyncExec(new Runnable() {
						public void run() {
							activar();
						}
					});
				//	activar();
				}
			}
			try {
				// Espera 10 segundos (¿cómo lo dejamos?)
				sleep(10000);
			} catch (Exception e) {
			}
		}
	}
	
	protected void activar() {		
				
		ArrayList<String> array = vista.getNombreDepartamentosJefe(vista.getEmpleadoActual());
		if (array != null) {
			for (int i = 0; i < array.size(); i++) {
				cmbDepartamentos.add(array.get(i));
			}
		}
		cmbDepartamentos.select(0);
		
		cmbDepartamentos.setEnabled(true);

		try {
			ArrayList<ArrayList<String>> a=vista.infoDpto2(cmbDepartamentos.getText());
			for (int i = 0; i < a.size(); i++) {
				TableItem tItem = new TableItem(tablaInfo, SWT.NONE);
				if(a.get(i).get(0)=="Hombre"){
					tItem.setImage(vista.getImagenes().getIco_chico());
				}else{
					tItem.setImage(vista.getImagenes().getIco_chica());
				}
				tItem.setText(1,a.get(i).get(3));
				tItem.setText(2,a.get(i).get(1));
				tItem.setText(3,a.get(i).get(4));
				if(a.get(i).get(2).equals("2")){
					tItem.setText(4,bundle.getString("I10_lab_jefe"));
				}else{
					tItem.setText(4,bundle.getString("Empleado"));
				}

}

		} catch (Exception e) {
			e.printStackTrace();
		}
		
		//asigno el tab del control de personal para cada dia
	//	fCentro.getItem(0).setControl(cInfoHorario.getControl());
	//	fCentro.getItem(1).setControl(calendario.getComposite());
		
		//Escribo el texto en el label
		//lContenido.setText(vista.infoDpto(cmbDepartamentos.getText()));

		// falta esta zona de rellenar
		// ico/sex, num, nom,horario

		
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	/**
	 * Crea un tab con un listado de departamentos
	 * 
	 * @param tabFolder el tabFolder donde colocarlo
	 * @author Carlos Sánchez, Agustín y Alberto Benayas
	 */
	private void crearTabAdminDepartamentos() {
		
		TabItem tabItemDepartamentos = new TabItem(tabFolder, SWT.NONE);
		tabItemDepartamentos.setText(bundle.getString("Departamentos"));
		tabItemDepartamentos.setImage(vista.getImagenes().getIco_chicos());

		final Composite cDepartamentos = new Composite(tabFolder, SWT.NONE);
		tabItemDepartamentos.setControl(cDepartamentos);

		cDepartamentos.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
				true, 1, 1));
		cDepartamentos.setLayout(new GridLayout(3, false));

		Label lDepartamentos = new Label(cDepartamentos, SWT.LEFT);
		lDepartamentos.setText(bundle.getString("Departamento"));
		lDepartamentos.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));

		cmbDepartamentos = new Combo(cDepartamentos, SWT.BORDER
				| SWT.READ_ONLY);
		cmbDepartamentos.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				false, 1, 1));

		// ArrayList<String> array = vista.getEmpleadoActual()
		// .getDepartamentosId();
		ArrayList<String> array = vista.getNombreTodosDepartamentos();
		if (array != null) {
			for (int i = 0; i < array.size(); i++) {
				cmbDepartamentos.add(array.get(i));
			}
		}
		cmbDepartamentos.select(0);
				
		// Composite for Buttons: "New Department" and "Configure Department"
		Composite cBut = new Composite(cDepartamentos, SWT.LEFT);
		cBut.setLayout(new GridLayout(2, false));

		// Button "Configure Department"
		Button bConfig = new Button(cBut, SWT.PUSH);
		bConfig.setText(bundle.getString("I02_but_Config_dep"));
		bConfig.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1,
				1));

		bConfig.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event e) {
				new ShellConfigDepartamento(tabFolder.getShell(), bundle, vista,
						cmbDepartamentos.getText(),cmbDepartamentos,true);
			}
		});

		// Button "New Department"
		Button bNew = new Button(cBut, SWT.PUSH);
		bNew.setText(bundle.getString("I02_but_Nuevo_dep"));
		bNew.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
		bNew.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event e) {
				new NuevoDepartamento(tabFolder.getShell(),bundle,vista,cmbDepartamentos);
			}
		});
		
		// Button "Delete Department"
		Button bDel = new Button(cBut, SWT.PUSH);
		bDel.setText(bundle.getString("I02_but_Del_dep"));
		bDel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
		bDel.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event e) {
				if(vista.tieneEmpleados(cmbDepartamentos.getText())){
					MessageBox messageBox = new MessageBox (tabFolder.getShell(), SWT.APPLICATION_MODAL | SWT.CLOSE | SWT.ICON_INFORMATION);
					messageBox.setText (bundle.getString("Mensaje"));
					messageBox.setMessage (bundle.getString("I02_err_elim"));
					e.doit = messageBox.open () == SWT.CLOSE;
				}else{//si no tiene ningun empleado
					MessageBox messageBox = new MessageBox (tabFolder.getShell(), SWT.APPLICATION_MODAL | SWT.OK | SWT.CANCEL | SWT.ICON_INFORMATION);
					messageBox.setText (bundle.getString("Mensaje"));
					messageBox.setMessage (bundle.getString("I02_confirm_elim_dep"));
					if(messageBox.open () == SWT.OK){
						if(!vista.eliminaDepartamento(cmbDepartamentos.getText())){
							MessageBox messageBox2 = new MessageBox (tabFolder.getShell(), SWT.APPLICATION_MODAL | SWT.OK | SWT.CANCEL | SWT.ICON_INFORMATION);
							messageBox2.setText (bundle.getString("Mensaje"));
							messageBox2.setMessage (bundle.getString("I10_err_elim_dep"));
							messageBox2.open ();
						}
						else{
							cmbDepartamentos.remove(cmbDepartamentos.getText());
							if(cmbDepartamentos.getItemCount()>0) cmbDepartamentos.select(0);
							MessageBox messageBox2 = new MessageBox (tabFolder.getShell(), SWT.APPLICATION_MODAL | SWT.OK | SWT.ICON_INFORMATION);
							messageBox2.setText (bundle.getString("Mensaje"));
							messageBox2.setMessage (bundle.getString("I10_ok_elim_dep"));
							messageBox2.open ();
						}
					}
				}
			}
		});

		Composite cInfo = new Composite(cDepartamentos, SWT.BORDER);
		cInfo.setLayout(new GridLayout(2, false));
		cInfo.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 3, 1));
	
	   tablaInfo=new Table(cInfo,SWT.MULTI | SWT.BORDER
				| SWT.FULL_SELECTION);
		tablaInfo.setLinesVisible(true);
		tablaInfo.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,true, 4, 1));
		tablaInfo.setHeaderVisible(true);
		String[] titles2 = { " ", bundle.getString("I02_lab_NVend"),
				bundle.getString("Nombre"),bundle.getString("Horario"),bundle.getString("Rango")};
		for (int i = 0; i < titles2.length; i++) {
			TableColumn column = new TableColumn(tablaInfo, SWT.NONE);
			column.setText(titles2[i]);
		}
		tablaInfo.addControlListener(new ControlListener() {
			public void controlResized(ControlEvent e) {
				// Configurar tamaño de las columnas
				int ancho = tablaInfo.getSize().x ;
				tablaInfo.getColumn(0).setWidth(20);				// ico			x
				tablaInfo.getColumn(1).setWidth(ancho/20*3);		// nvend		15
				tablaInfo.getColumn(2).setWidth(ancho/5*2);		// nombre		40
				tablaInfo.getColumn(3).setWidth(150);		// horario	15
				tablaInfo.getColumn(4).setWidth(100);
			}
			public void controlMoved(ControlEvent e) {};
		});
		// falta esta zona de rellenar
		// ico/sex, num, nom,horario
		ArrayList<ArrayList<String>> a=vista.infoDpto2(cmbDepartamentos.getText());
		for (int i = 0; i < a.size(); i++) {
			TableItem tItem = new TableItem(tablaInfo, SWT.NONE);
			if(a.get(i).get(0)=="Hombre"){
				tItem.setImage(vista.getImagenes().getIco_chico());
			}else{
				tItem.setImage(vista.getImagenes().getIco_chica());
			}
			tItem.setText(1,a.get(i).get(3));
			tItem.setText(2,a.get(i).get(1));
			tItem.setText(3,a.get(i).get(4));
			if(a.get(i).get(2).equals("2")){
				tItem.setText(4,bundle.getString("I10_lab_jefe"));
			}else{
				tItem.setText(4,bundle.getString("Empleado"));
			}
		
		/*TableItem tItem = new TableItem(tablaInfo, SWT.NONE);
		tItem.setImage(vista.getImagenes().getIco_chico());
		tItem.setText(1,"HOla" );
		tItem.setText(2,"HOLA" );*/
		
		//listener para el combo y mostrar la info debajo
		cmbDepartamentos.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event e) {
				ArrayList<ArrayList<String>> a=vista.infoDpto2(cmbDepartamentos.getText());
				for (int i = 0; i < a.size(); i++) {
					TableItem tItem = new TableItem(tablaInfo, SWT.NONE);
					if(a.get(i).get(0)=="Hombre"){
						tItem.setImage(vista.getImagenes().getIco_chico());
					}else{
						tItem.setImage(vista.getImagenes().getIco_chica());
					}
					tItem.setText(1,a.get(i).get(3));
					tItem.setText(2,a.get(i).get(1));
					tItem.setText(3,a.get(i).get(4));
					

			}
			}
		});

	}
}}
